import 'dart:convert';

import 'package:star_author/utils/common_util/log_util.dart';
import 'package:star_author/utils/common_util/shared_preferences/sp_keys_constant.dart';
import 'package:star_author/utils/common_util/shared_preferences/sp_util.dart';
import 'package:flutter/material.dart';

class GuideUserUtil {
  static const String notice_tab_page = "notice_tab_page";
  static const String mine_tab_page = "mine_tab_page";
  static const String wallet_page = "wallet_page";
  static const String my_back_flows_list_page = "my_back_flows_list_page";
  static const String bidder_goods_detaill_page = "bidder_goods_detaill_page";
  static const String create_backflow_result_page =
      "create_backflow_result_page";
  static const String order_sure_page = "order_sure_page";
  static const String backflow_back_dialog = "backflow_back_dialog";

  static Map _firstOpenGuideIndexMap = {
    notice_tab_page: 0,
    mine_tab_page: 0,
  };

  ///获取引导下标
  ///-1表示完成所有引导
  static int getLastIndex(String key) {
    Map map = getSpInfoMap();
    int firstOpenGuideIndex = map[key] ?? 0;

    LogUtil.v("FirstOpenGuideUtil-> getLastIndex : $map");
    return firstOpenGuideIndex;
  }

  static Map getSpInfoMap() {
    Map map = _firstOpenGuideIndexMap;
    String tempStr = SpUtil.getString(SpKeysConstant.firstOpenGuideIndex,
        defValue: json.encode(map))!;
    map = json.decode(tempStr);
    return map;
  }

  static void next(String key) {
    int currentIndex = getLastIndex(key) + 1;
    LogUtil.v("FirstOpenGuideUtil-> next ->currentIndex: $currentIndex");

    Map map = getSpInfoMap();
    map[key] = currentIndex;

    LogUtil.v("FirstOpenGuideUtil-> next : $map");

    SpUtil.putString(SpKeysConstant.firstOpenGuideIndex, json.encode(map));
  }

  ///开启引导
  static void startShowCase(
      BuildContext context, List<GlobalKey> globalKeyList, String keyName,
      {Function? beforeCallback}) {
    int index = GuideUserUtil.getLastIndex(keyName);

    if (index + 1 <= globalKeyList.length) {
      beforeCallback?.call();

      //记录上次下标，从上次开始
      List<GlobalKey> tempList = globalKeyList;
      tempList.sublist(index);
      // ShowCaseWidget.of(context)?.startShowCase(globalKeyList);
    }
  }
}
